/*
 * Copyright (C) 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cellbots.local;

import com.cellbots.LauncherActivity;
import com.cellbots.RobotEntry;
import com.cellbots.RobotList;
import com.cellbots.local.RobotBtController;
import com.cellbots.local.robotcontrollerservice.AbstractRobotControllerService;
import com.cellbots.local.robotcontrollerservice.CreateRobotControllerService;
import com.cellbots.local.robotcontrollerservice.DefaultRobotControllerService;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

/**
 * Class for the robot controller for all robots that conform with the motor
 * command spec, and thus respond to the same commands that CellDroid receives
 * from the remote controller. Thus class just forwards the commands received
 * from the remote controller, to the robot over Bluetooth.
 *
 * @author chaitanyag@google.com (Chaitanya Gharpure)
 */
public class RobotControllerService extends AbstractRobotControllerService {

	private static final String TAG = "DefaultRobotControllerService";
	static  String un;
	static String rbtn;
	static String rbta;
	private RobotBtController robotController;

	/** Called when the activity is first created. */
	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent intent) {
		Log.d("balnk","onBind");
		return mBinder;
	}

	/**
	 * Stub for exposing the service's interface.
	 */
	private final AbstractRobotControllerService.Stub mBinder =
			new AbstractRobotControllerService.Stub() {
        @Override
        public void connect(String username, String robotBtName, String robotBtAddr) {
            super.connect(username, robotBtName, robotBtAddr);
            un = username;
            rbtn = robotBtName;
            rbta = robotBtAddr;
            new Thread(new Runnable() {
                @Override
                public void run() {
                		Looper.prepare();
                    Log.d("cellbots RobotContorller","RobotBtController()");
                    if (robotController == null)
                    		robotController = new RobotBtController(un, rbtn, rbta);                		
                		Log.d("cellbots RobotContorller run()","startConnection()");
                    if (robotController.startConnection()) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            mCurrentState = STATE_BLUETOOTH_FAIL;
                            return;
                        }
                        //sendStartCommand();
                        mCurrentState = STATE_SUCCESS;
                        return;
                    } else {
                        mCurrentState = STATE_BLUETOOTH_FAIL;
                    }
                }
            }).start();
        }


       @Override
        public void connect2(){
        	SharedPreferences mPrefs;
        	RobotList mRobotProfiles;
        	RobotEntry mEntry;
        	String username,robotBtName,robotBtAddr;
        	int index=0;
          	
            //get RobotEntry index=0           
            mPrefs = getSharedPreferences(LauncherActivity.PREFERENCES_NAME, MODE_PRIVATE);
            mRobotProfiles = new RobotList(mPrefs);
            mRobotProfiles.load();
                        
            mEntry = mRobotProfiles.getEntry(index);
            
            //get Strings for connect
            username = mEntry.getType();
            robotBtName = mEntry.getBluetoothName();
            robotBtAddr = mEntry.getBluetoothAddr();
            
            // call original connect()
            Log.d("cellbots username",username);
            Log.d("cellbots robotBtName",robotBtName);
            Log.d("cellbots robotBtAddr",robotBtAddr);
            connect(username, robotBtName, robotBtAddr);
            
        }
		
		
		@Override
        public void clean(){
        	// send comand "clean" to roomba
			Log.d("cellbots ","before clean()");
        		//byte[] startCmd = new byte[1];
            //startCmd[0] = (byte) (87); //135: Clean
            if (robotController != null && mCurrentState == STATE_SUCCESS)
                robotController.write("f");            
            //robotController.write(startCmd);
            Log.d("cellbots ","after clean()");
        }
		
		@Override
		public void disconnect() {
			super.disconnect();
			if (robotController != null)
				robotController.disconnect();
		}

		@Override
		public void sendCommand(String cmd) {
			super.sendCommand(cmd);
			if (robotController != null && mCurrentState == STATE_SUCCESS)
				robotController.write(cmd);
		}

		@Override
		public String test(){
			Log.d("balnk","test()");
			return "test1";
		}
	};
}
